$().ready(function() {
	$('.contract-page').livequery(function() {
		$("#response-link").click(function(event) {
			$(this.parentNode).hide();
			$('#to-response').slideDown();
			event.stopPropagation();
		});
		$("a#to-sign-contract").fancybox({
			autoSize: false,
			height: 50,
			width: 380,
			helpers : {
				overlay : {
					opacity: 0.73
				}
			},
			scrolling: 'no',
			onStart: function() {
				this.content.html();
			}
		});
		$("a#not-sign-contract").fancybox({
			autoSize: false,
			height: 115,
			width: 433,
			helpers : {
				overlay : {
					opacity: 0.73
				}
			},
			scrolling: 'no',
			onStart: function() {
				this.content.html();
			}
		});
		$("#sign-yes").click(function() {
			$.ajax({
				dataType: 'json',
				url: W_AJAX + 'contract/accept_contract',
				type: 'POST',
				data: {
					ID_contract: $('#contract-full').attr('pk')
				},
				success: function(data) { console.log(data);
					if(data.result == 'OK') {
						$("#to-sign-contract, #not-sign-contract").remove();
						alert('Договор подписан');
						if ($("#new-all-count").text() == 1) $("#new-all-count").remove();
						else if($("#new-all-count").text() > 1 ) $("#new-all-count").text(data.currentAllNewEventsCount);
						if ($("#new-contract-events-count").text() == 1) $("#new-contract-events-count").remove();
						else if($("#new-contract-events-count").text() > 1 ) $("#new-contract-events-count").text(data.currentContractNewEventsCount);
						$.fancybox.close();
					}
				}	
			});
		});
		$("#sign-no").click(function() {
			$.ajax({
				dataType: 'json',
				url: W_AJAX + 'contract/not_accept_contract',
				type: 'POST',
				data: {
					ID_contract: $('#contract-full').attr('pk')
				},
				success: function(data) {
					if(data.result == 'OK') {
						$("#to-sign-contract, #not-sign-contract").remove();
						alert('Вы ответили отказом на договор');
						if ($("#new-all-count").text() == '1') $("#new-all-count").remove();
						else if($("#new-all-count").text() > 1 ) $("#new-all-count").text(data.currentAllNewEventsCount);
						if ($("#new-contract-events-count").text() == '1') $("#new-contract-events-count").remove();
						else if($("#new-contract-events-count").text() > 1 ) $("#new-contract-events-count").text(data.currentContractNewEventsCount);
						$.fancybox.close();
					}
				}	
			});
		});
		$(".no").click(function() {
			$.fancybox.close();
		});
		function closeCommentBlock() {
			$('#to-response').hide('slide');
			$('.userError').remove();
			$("#phantom-block").show();
		}
		$('body').click(function(event){
			var block = $(event.target).closest('#to-response');
			var ignoredBlock = $('.userError','#response-form');
			if(!block.length){
				closeCommentBlock();
			}
		});
		$(function(){
			$('.field').fieldClear();
			$('.mandatory').hideErrors();
		});
		$('#response-form').submit(function() { 
			$objForm = $(this);
			if($objForm.fullValidate()) {
				$.ajax({
					dataType: 'json',
					url: W_AJAX + 'contract/response',
					type: 'POST',
					data: {
						text: $('#ContractResponse_text',$objForm).val(),
						ID_contract: $('#contract-full').attr('pk')
					},
					success: function(data) {
						if(data.result == 'OK') {
							closeCommentBlock();
							$('#response-link').remove();
							alert('Отзыв добавлен');
						}
					}	
				});
			}
			return false;
		});
	});
});